package server

import (
	"log"
	"server"
)

func (s *Server) onTextMessage(id string, m *Pack) {
	cmd := m.Cmd

	_, ok := s.conns[id]

	if !ok {
		if cmd == "login" {
			s.onLogin(id, m)
		} else {
			log.Println("invalid package: %s", m)
		}
		return
	}

	// Conn that is already logged in
	if cmd == "login" {
		// Ignore duplicate login command
		log.Println("ignoring duplicated login command")
		return
	}

	if cmd == "request" ||
		cmd == "response" ||
		cmd == "start_binary" ||
		cmd == "end_binary" {
		s.proxyPack(id, m)
		return
	}

	if cmd == "binary" {
		s.onBinary(id, m)
	} else if cmd == "push" {
		s.onPush(id, m)
	} else if cmd == "publish" {
		s.onPublish(id, m)
	} else if cmd == "find" {
		s.onFind(id, m)
	} else if cmd == "subscribe" {
		s.onSubscribe(id, m)
	} else if cmd == "unsubscribe" {
		s.onUnsubscribe(id, m)
	}
}
